﻿
function extraInit()
{
    initTree(true, true, true);
}

function initTree(contextmenu, hotkey, dnd)
{
    var plugins = ["themes","html_data","ui","crrm"];
    if (contextmenu){
        plugins[plugins.length] = "contextmenu";
    }
    if (hotkey){
        plugins[plugins.length] = "hotkeys";
    }
    if (dnd){
        plugins[plugins.length] = "dnd";
    }
    $(".systree")
		// call `.jstree` with the options object
		.jstree({
			// the `plugins` array allows you to configure the active plugins on this instance
			"plugins" : plugins,
			"ui":{"select_limit" : 1},
			// each plugin you have included can have its own config object
			"core" : { "open_parents" : true },
			// it makes sense to configure a plugin only if overriding the defaults
			"contextmenu" : {
			    "items" : function(node){
			      return createTreeContextMenu(node);
                }
            },
            "dnd" : {"copy_modifier":false} 
		})
		// EVENTS
		// each instance triggers its own events - to process those listen on the container
		// all events are in the `.jstree` namespace
		// so listen for `function_name`.`jstree` - you can function names from the docs
		.bind("loaded.jstree", function (event, data) {
			// you get two params - event & data - check the core docs for a detailed description
			$.jstree._reference(emptyId).open_all(emptyId);
		})
		.bind("rename.jstree", function (e, data) {
			renameTreeNode(e, data);
		})
		.bind("remove.jstree", function (e, data) {
		    removeTreeNode(e, data);
	    })
	    .bind("create.jstree", function (e, data) {
		    createTreeNode(e, data);
	    })
	    .bind("move_node.jstree", function (e, data) {
		    moveTreeNode(e, data);
	    })
	    .bind("select_node.jstree", function (event, data) { 
		    // `data.rslt.obj` is the jquery extended node that was clicked
		    var nodeId = data.rslt.obj.attr("id");
		    //var nodeId = $("a.jstree-clicked").closest("li").attr("id");
		    selectTreeNode(nodeId);
	    });
}

function createTreeContextMenu(node)
{
    var key = node.attr("id");
			      
      var items = new Array();
      
      var create = new Object();
      create.separator_before = false;
      create.separator_after = true;
      create.label = "Create";
      create.action = function (obj) { this.create(obj); };
      items[0] = create;
      
      if (key != emptyId)
      {
          var rename = new Object();
          rename.separator_before = false;
          rename.separator_after = true;
          rename.label = "Rename";
          rename.action = function (obj) { this.rename(obj); };
          items[1] = rename;
          
          var remove = new Object();
          remove.separator_before = false;
          remove.separator_after = true;
          remove.label = "Delete";
          remove.action = function (obj) {
            if(this.is_selected(obj))
            {
                this.remove();
            }
            else
            {
                this.remove(obj);
            }
          };
          items[2] = remove;
      }
      
      var ccp = new Object();
      ccp.separator_before = false;
      ccp.separator_after = true;
      ccp.label = "Edit";
      ccp.action = false;
      ccp.submenu = new Array();
      ccp.submenu[0] = new Object();
      ccp.submenu[0].separator_before = false;
      ccp.submenu[0].separator_after = false;
      ccp.submenu[0].label = "Cut";
      ccp.submenu[0].action = function (obj) { this.cut(obj); };
      ccp.submenu[1] = new Object();
      ccp.submenu[1].separator_before = false;
      ccp.submenu[1].separator_after = false;
      ccp.submenu[1].label = "Copy";
      ccp.submenu[1].action = function (obj) { this.copy(obj); };
      ccp.submenu[2] = new Object();
      ccp.submenu[2].separator_before = false;
      ccp.submenu[2].separator_after = false;
      ccp.submenu[2].label = "Paste";
      ccp.submenu[2].action = function (obj) { this.paste(obj); };
      //items[3] = ccp;
      return items;
}

function renameTreeNode(e, data)
{
}

function removeTreeNode(e, data)
{
}

function createTreeNode(e, data)
{
}

function moveTreeNode(e, data)
{
}

function selectTreeNode(nodeId)
{
}